var pageSize = 10;
//会诊类别
var conClassMap = {};
//会诊状态
var conStatusMap = {};
//列表类型
var listType=null;
//列表类型文字
var listTypeText=null;
//列表类型字典
var listTypeMap={
	"qcBefore" : "1",
	"assign" : "2",
	"myTask" : "3",
	"qcAfter" : "4",
	"myApply" : "5"
};

$(document).ready(function(){
	commonSet();
	//初始化查询条件
	initQueryCondition();
	//待办表格初始化
	initUnfinishTable();
	//已办表格初始化
	initFinishTable();
	//事件绑定
	eventBind();
});

function commonSet(){
	initLoader();
	initDatePickers();
	listTypeText=$("#listType").val();
	listType=listTypeMap[listTypeText];
	//会诊类别
	conClassMap =getTransParam('conClasseInfo');
	//会诊状态
	conStatusMap=getTransParam('conStatusInfo');
}

//初始化查询条件
function initQueryCondition(){
	//设置起始结束日期
	var now=new Date();
	var nowAfter1month=$.dateAdd(now,'month',1);
	var nowBefore1month=$.dateAdd(now,'month',-12);
	$("input[name='start_date']").val($.dateFormat(nowBefore1month, "yyyy-MM-dd"));
	$("input[name='end_date']").val($.dateFormat(nowAfter1month, "yyyy-MM-dd"));
	
	genDictSelect('conClasseInfo','unfinishConclass','',{code:'',value:'请选择'});
	genDictSelect('conClasseInfo','finishConclass','',{code:'',value:'请选择'});
}
//事件绑定
function eventBind(){
	$("#unfinishQueryBtn").click(unfinishPageQuery).click();
	$("#finishQueryBtn").click(finishPageQuery).click();
}

//待办表格初始化
function initUnfinishTable(){
	$('#unfinishConTable').bootstrapTable({
		onPageChange: function (number, size) {
			pageSize = size;
			unfinishQuery(number, size);
		}
	});
}

//已办表格初始化
function initFinishTable(){
	$('#finishConTable').bootstrapTable({
		onPageChange: function (number, size) {
			pageSize = size;
			finishQuery(number, size);
		}
	});
}

//待办带有分页信息的查询
function unfinishPageQuery(){
	unfinishQuery(1, pageSize);
};

//待办带有分页信息的查询
function finishPageQuery(){
	finishQuery(1, pageSize);
};

//待办分页查询
function unfinishQuery(pageNumber, pageSize){
	var queryCondition={
		listType:listType,
		pageNumber: pageNumber, 
		pageSize: pageSize,
		params:$('#unfinishQueryForm').collect()
	}
	
	$.getApiClient('/consultation/list/{listType}/unfinish').setQuery(queryCondition).get().done(function(data){
	    $('#unfinishConTable').bootstrapTable('load', data.payload);
	});
};

//已办分页查询
function finishQuery(pageNumber, pageSize){
	var queryCondition={
		listType:listType,
		pageNumber: pageNumber, 
		pageSize: pageSize,
		params:$('#finishQueryForm').collect()
	}
	
	$.getApiClient('/consultation/list/{listType}/finish').setQuery(queryCondition).get().done(function(data){
	    $('#finishConTable').bootstrapTable('load', data.payload);
	});
};

//操作列
function operateFormatter(value, row, index) {	
	var operates=[];
	operates.push('<a class="edit" href="javascript:void(0)" title="查看"><i class="fa fa-edit"></i>查看</a>');
	return operates.join('');
};

//会诊状态格式转换
function statusFormatter(value, row, rowindex) {
	if (!this.formatterArgs) {
		return value;
	}
	var backLable='';
	var statusText=window[this.formatterArgs][value] || null;
	if(row.reason_id){
		backLable='<span class="label label-danger">退回</span>';
	}
	return statusText+'&nbsp;'+backLable;
}

unfinishOperateEvents = {
	'click .edit' : function(e, value, row, index) {
		var isBack=row.reason_id? '1':'0';
		redirectCall('/page/business/consultation/detail?type='+listTypeText+'&conId='+row.id+'&status='+row.con_status+'&isBack='+isBack,'/page/business/consultation/list?type='+listTypeText);
	}
};

finishOperateEvents = {
	'click .edit' : function(e, value, row, index) {
		redirectCall('/page/business/consultation/detail?type='+listTypeText+'&conId='+row.id+'&status='+row.con_status,'/page/business/consultation/list?type='+listTypeText);
	}
};